feat(frontend): add context window usage tracking#825
feat(frontend): add context window usage tracking#825jeremyeder wants to merge 2 commits intomainfrom
Conversation
23dd182 to
5a02810
Compare
|
placeholder |
|
hello |
|
Test line one |
Test HeaderSome content |
|
Claude Code Review Summary This PR adds context window usage tracking via RUN_FINISHED events and a sparkline component. Clean implementation overall, but contains one standards violation and two functional correctness issues to address before merge. Blocker Issues: None. |
|
Claude Code Review CRITICAL: Use type not interface. ContextUsage.tsx line 16. MAJOR 1: Accumulation bug. event-handlers.ts lines 279-288. MAJOR 2: Hardcoded limit 200K. types.ts line 50. MAJOR 3: Unbounded perTurn. event-handlers.ts line 287. |
|
Claude Code Review - Part 1 of 4: Summary and Critical Issues Summary: This PR adds context window usage tracking from RUN_FINISHED events with a sparkline display. Clean implementation overall with one standards violation and two correctness issues. CRITICAL - interface instead of type for props |
|
Claude Code Review - Part 2 of 4: Major Issues MAJOR 1 - Token accumulation semantics are incorrect MAJOR 2 - Hardcoded context limit ignores model selection |
|
Claude Code Review - Part 3 of 4: Remaining Issues MAJOR 3: perTurn grows unbounded. Cap the array on each write to prevent long sessions from accumulating thousands of state entries. MINOR 1: Array index as React key in sparkline bars - use a stable key derived from position in the full perTurn array. MINOR 2: Per frontend-development.md single-use components should be colocated with their page. Please confirm whether ContextUsage is only used in one place. |
|
Recommendations: 1 Fix token accumulation. 2 Fix interface to type. 3 Cap perTurn array. 4 Address hardcoded limit. 5 Fix array index key. |
|
Positives: Correct unknown-cast pattern for event assertions. Inline object type in agui.ts is correct. Graceful no-op when usage data is absent. Division-by-zero guard in sparkline. Clean formatTokens helper. |
Merge Readiness — Blockers Found\n\n| Check | Status | Detail |\n|-------|--------|--------|\n| CI | FAIL | Failing: lint-frontend, build-and-push (frontend, ./components/frontend, quay.io/ambient_code/vteam_frontend, ./component..., lint-summary (+1 more) |\n| Merge conflicts | pass | — |\n| Review comments | needs_review | Has comments — agent to evaluate |\n| Jira hygiene | warn | No Jira reference found |\n| Staleness | pass | — |\n| Diff overlap risk | pass | — |\n\n> This comment is auto-generated by the PR Overview workflow and will be updated when the PR changes.\n\n |
Track cumulative token usage from RUN_FINISHED events and display as a sparkline indicator showing tokens per turn. - Add contextUsage field to AGUIClientState - Add PlatformRunFinishedEvent type with result.usage shape - Add getContextLimit() utility for dynamic model-based limits - Extract input_tokens + output_tokens from RUN_FINISHED result - Add ContextUsage component with token count and sparkline Context limits: - claude-opus-4*: 1M tokens - claude-sonnet*, claude-haiku*: 200K tokens (default) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
b064f7b to
8848df4
Compare
This comment has been minimized.
This comment has been minimized.
|
Claude Code Review for PR 825 - context-usage-indicator |
|
Claude Code Review Summary This PR introduces context window usage tracking for the frontend session UI. It adds token accumulation in the RUN_FINISHED event handler, a new ContextUsage sparkline component, and model-aware context limit lookup. The implementation is clean and well-typed. One functional accuracy issue with the Opus 4 context limit and a few minor style points are worth addressing. Blocker Issues: None Critical Issues: None Major Issues
File: components/frontend/src/types/agui.ts (line ~296) Problem: claude-opus-4 is mapped to 1,000,000 tokens, but Claude Opus 4 (including claude-opus-4-6, the model ID this platform defaults to) has a 200K context window, the same as Sonnet and Haiku. The prefix check model.startsWith(claude-opus-4) will match claude-opus-4-6 and show 83K/1M instead of the correct 83K/200K, making the indicator actively wrong for the default model. Standard violated: Code quality, functional correctness of the core feature. Suggested fix: Verify against Anthropic published context limits. If all Claude 4.x models are 200K, remove the Opus 1M entry. If a 1M variant genuinely exists, use its exact model ID prefix rather than the generic claude-opus-4 prefix. Minor Issues
Positive Highlights Clean TypeScript throughout: no any anywhere in the diff, all new types use the type keyword. Excellent type extension pattern: PlatformRunFinishedEvent = RunFinishedEvent and { result?: ... } augments the base type cleanly without forking. Graceful degradation: getContextLimit falls back to DEFAULT_CONTEXT_LIMIT and the event handler guards on result?.usage, so missing usage data never crashes. Sensible UX: capping the sparkline to 24 turns and guarding on recent.length > 0 keep the display clean from the start. formatTokens handles raw, K, and M scale tiers correctly and concisely. Recommendations
|
The ContextUsage component was implemented but never imported or rendered in the session UI. This adds the missing import and renders the component in the header area next to the session kebab menu. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Claude Code ReviewSummaryThis PR adds context window usage tracking to the session UI, including a new Issues by SeverityBlocker IssuesNone Critical IssuesNone Major Issues1.
if (event.result?.usage) {
const { input_tokens = 0, output_tokens = 0 } = event.result.usage
state.contextUsage = {
// input_tokens of the last turn == current context window occupancy
used: input_tokens,
perTurn: [...state.contextUsage.perTurn, input_tokens + output_tokens],
}
}The sparkline ( Minor Issues1. Array index used as React
// Compute start offset before the map
const offset = state.contextUsage.perTurn.length - recent.length
// ...
{recent.map((tokens, i) => (
<div key={offset + i} ... />
))}2.
Positive Highlights
Recommendations
🔍 View AI decision process (logs available for 90 days) 📋 View memory system files loaded (click to expand)What Amber Loaded for Code ReviewAmber automatically loaded these repository standards from the memory system:
Impact: This review used your repository's specific code quality standards, security patterns, and best practices from the memory system (PRs #359, #360) - not just generic code review guidelines. |
|
a wreck of an implementation attempt. i stopped caring about this since switching to the 1m context 4.6 |
Summary
RUN_FINISHEDeventsChanges
types/agui.tscontextUsage,PlatformRunFinishedEvent,getContextLimit()hooks/agui/types.tscontextUsageininitialStatehooks/agui/event-handlers.tsRUN_FINISHED.resultcomponents/session/ContextUsage.tsxUsage
Renders:
83K/200K ▁▂▃▄▂▃▁▂▄▃▂▄Context Limits
claude-opus-4*claude-sonnet*,claude-haiku*Test plan
result.usageis missing🤖 Generated with Claude Code